package com.cloudkinto.appletController.user;


import com.cloudkinto.appletservice.user.AppletUserService;
import com.cloudkinto.appletservice.user.vo.*;
import com.cloudkinto.base.BaseController;
import com.cloudkinto.common.annotations.UserSessionRequired;
import com.cloudkinto.common.web.SingleResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 张永远
 * @since 2020-05-27
 */
@RestController
@Api(tags = "小程序用户模块(前台）")
@RequestMapping("/api/applet/user")
public class AppletUserController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(AppletUserController.class);

    @Autowired
    private AppletUserService  service;


    @PostMapping("login")
    @ApiOperation(value = "绑定并登录", notes = "获取token")
    public SingleResult login(@RequestBody @Validated AppletUserLoginReq req) {
        return SingleResult.success(service.login(req));
    }

    @PostMapping("wxDecryptData")
    @ApiOperation(value = "换取手机号等加密信息", notes = "手机号/基本信息")
    public SingleResult wxTelPhone(@RequestBody @Validated AppletDecryptReq req) throws Exception{
        return SingleResult.success(service.decryptData(req));
    }

    @GetMapping("sendVerificationCode")
    @ApiOperation(value = "获取验证码", notes = "获取验证码")
    public SingleResult sendVerificationCode(String telPhone) {
        return service.sendVerificationCode(telPhone);
    }

    @UserSessionRequired
    @PostMapping("logout")
    @ApiOperation(value = "用户退出登录", notes = "用户退出登录")
    public SingleResult logout(@RequestBody LogOutReq logoutReq) {
        service.logout(logoutReq);
        return SingleResult.success("退出成功",null);
    }

    @PostMapping("register")
    @ApiOperation(value = "用户注册", notes = "Stoc用户注册")
    public SingleResult register(@RequestBody @Validated AppletUserRegisterReq req) {
        return SingleResult.success("注册成功", service.register(req));
    }

    @GetMapping("bindWx")
    @ApiOperation(value = "绑定微信验证", notes = "绑定小程序")
    public SingleResult bingWx(String code) {
        return service.appletAccountVerify(code);
    }

    @GetMapping("detailInfo")
    @ApiOperation(value = "用户资料", notes = "绑定小程序")
    public SingleResult detailInfo(Long id) {
        return service.detailInfo(id);
    }

    @UserSessionRequired
    @PutMapping("detailInfoPhone")
    @ApiOperation(value = "修改个人信息-手机号", notes = "修改")
    public SingleResult updatePhone(@RequestBody @Validated AppletUserUpdateReq req) {
        Long userId = getCurrentUserId();
        return SingleResult.success("修改成功", service.updateUserInfoPhone(req));
    }

    @UserSessionRequired
    @PutMapping("detailInfoMail")
    @ApiOperation(value = "修改个人信息-邮箱", notes = "修改")
    public SingleResult updateMail(@RequestBody @Validated AppletUserUpdateReq req) {
        Long userId = getCurrentUserId();
        return SingleResult.success("修改成功", service.updateUserInfoMail(req));
    }

    @UserSessionRequired
    @PutMapping("detailInfoName")
    @ApiOperation(value = "修改个人信息-昵称", notes = "修改")
    public SingleResult updateName(@RequestBody @Validated AppletUserUpdateReq req) {
        Long userId = getCurrentUserId();
        return SingleResult.success("修改成功", service.updateUserInfoName(req));
    }

    @UserSessionRequired
    @PutMapping("updatePassword")
    @ApiOperation(value = "修改密码", notes = "修改")
    public SingleResult updatePassword(@RequestBody @Validated AppletUpdatePasswordReq req) {
        Long userId = getCurrentUserId();
        return SingleResult.success("修改成功", service.updatePassword(req));
    }

}

